home *** CD-ROM | disk | FTP | other *** search
/ The Arsenal Files 8 / The Arsenal Files Collection #8 (Arsenal Computer) (1996).ISO / g_quake / xpak041.zip / WILDMAT.PAS < prev    next >
Pascal/Delphi Source File  |  1996-09-30  |  3KB  |  69 lines

  1. unit WildMat;
  2.  
  3. interface
  4.  
  5. function WildCardMatch(const WildCard, TestStr : string) : boolean ;
  6.  
  7. (* Many thanks to Dr John Stockton, jrs@merlyn.demon.co.uk *)
  8.  
  9. {
  10. >I was just wondering if you had a piece of code to match wildcards...
  11.  
  12. Not yet, unless there's one in BP7 distribution or SWAG.
  13.  
  14. >As I see it, the way to implement wildcards would be a function:
  15. >function WildCardMatch(FullText, WildText:string): boolean;
  16. >so  WildCardMatch('Hello','H*') = True
  17. >and WildCardMatch('tomw@tsys.demon.co.uk','*uk') = True
  18.  
  19. Agreed, though the parameters should if possible be "const", and I'd put
  20. them in the other order.
  21.  
  22. >
  23. > c) My guess is that recursion is the technique to swear by.  Do you also know
  24. >    of a routine which does not use recursion?
  25.  
  26. Yes.   No - seeing that I don't yet know of one anyhow!
  27.  
  28. Assuming '*' can be matched by '', both your examples can be done with
  29. Pos :  Pos('H', 'Hello')>0 ;
  30. (* to test if S1 is in a short list of words, I often use
  31. Pos(#32+S1, ' Word1 Word2 Word3 ...')>0 or similar. *)
  32.  
  33. Pause for a bit of programming :
  34.  
  35. I've changed it to pass Seek & Within unmodified and also to pass the
  36. current position reached in each.  It has been VERY SLIGHTLY tested.}
  37.  
  38. implementation
  39.  
  40. function WildCardMatch(const WildCard, TestStr : string) : boolean ;
  41. function WCM(const WildPos, TestPos : byte) : boolean ;
  42. { N.B. WildPos, TestPos point to the first relevant character in
  43.   Seek, Within; problems with 255-long strings might be avoided by
  44.   using instead the index of the last irrelevant character. }
  45. var NextLetter : byte ;
  46. begin
  47.  
  48.   if (WildPos>Length(WildCard)) and (TestPos>Length(TestStr)) then
  49.      begin WCM := true ; EXIT end;
  50.   if (WildPos>Length(WildCard)) or  (TestPos>Length(TestStr)) then
  51.      begin WCM := false; EXIT end;
  52.  
  53.   if WildCard[WildPos]='*' then begin
  54.     if Length(WildCard)=WildPos then begin WCM := true ; EXIT end ;
  55.     NextLetter:=Pos(WildCard[Succ(WildPos)], Copy(TestStr, TestPos, 255))
  56.       { Improve that line as a for/while/repeat without Copy!
  57.         & adjust other B if needed } ;
  58.     if NextLetter=0 then begin WCM := false ; EXIT end ;
  59.     if WCM(WildPos+2, TestPos+NextLetter) then begin WCM := true ; EXIT end ;
  60.     WCM := WCM(WildPos, TestPos+NextLetter) ;
  61.     EXIT end ;
  62.   if (WildCard[WildPos]='?') or (WildCard[WildPos]=TestStr[TestPos]) then begin
  63.     WCM := WCM(Succ(WildPos), Succ(TestPos)) ;
  64.     EXIT end ;
  65.   WCM := false end {WC} ;
  66. begin
  67.   {Writeln('wcm: ''',Wildcard,''',''',TestStr,''' (',Ord(TestStr[Length(Teststr)-1]),')');}
  68.   WildCardMatch := WCM(1,1) end;
  69. end.